package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.MesEquipmentExamineProjectRpcService;
import com.hunttown.mes.rpc.domain.MesEquipmentExamineProjectDTO;
import com.hunttown.mes.rpc.domain.query.MesEquipmentExamineProjectDTOQuery;
import com.hunttown.mes.service.MesEquipmentExamineProjectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.MesEquipmentExamineProjectConverts.convert;

/**
 * created by wangjunfu on 2022-04-19 10:12:57
 */
@Service
public class MesEquipmentExamineProjectRpcServiceImpl implements MesEquipmentExamineProjectRpcService {

    private final Logger logger = LoggerFactory.getLogger(MesEquipmentExamineProjectRpcServiceImpl.class);

    private final MesEquipmentExamineProjectService mesEquipmentExamineProjectService;

    @Autowired
    public MesEquipmentExamineProjectRpcServiceImpl(MesEquipmentExamineProjectService mesEquipmentExamineProjectService) {
        this.mesEquipmentExamineProjectService = mesEquipmentExamineProjectService;
    }

    @Override
    public MesEquipmentExamineProjectDTO insert(MesEquipmentExamineProjectDTO mesEquipmentExamineProjectDTO) {
        try {
            return convert(mesEquipmentExamineProjectService.insert(convert(mesEquipmentExamineProjectDTO)));
        } catch (Exception e) {
            logger.error("MesEquipmentExamineProject create failed.", e);
            throw new RuntimeException("MesEquipmentExamineProject create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(MesEquipmentExamineProjectDTO mesEquipmentExamineProjectDTO) {
        try {
            return mesEquipmentExamineProjectService.updateInfoById(convert(mesEquipmentExamineProjectDTO));
        } catch (Exception e) {
            logger.error("MesEquipmentExamineProject update by id failed.", e);
            throw new RuntimeException("MesEquipmentExamineProject update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(MesEquipmentExamineProjectDTOQuery mesEquipmentExamineProjectDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesEquipmentExamineProjectDTOQuery.getW_id() != null && mesEquipmentExamineProjectDTOQuery.getW_id() > 0) {
                query.put("w_id", mesEquipmentExamineProjectDTOQuery.getW_id());
            }

            if (mesEquipmentExamineProjectDTOQuery.getIds() != null && !mesEquipmentExamineProjectDTOQuery.getIds().equals("")) {
                query.put("ids", mesEquipmentExamineProjectDTOQuery.getIds());
            }

            if (mesEquipmentExamineProjectDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesEquipmentExamineProjectDTOQuery.getDeleteFlag());
            }

            return mesEquipmentExamineProjectService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("MesEquipmentExamineProject update by query failed.", e);
            throw new RuntimeException("MesEquipmentExamineProject update by query failed, Please order your code.", e);
        }
    }

    @Override
    public MesEquipmentExamineProjectDTO selectById(Integer id) {
        try {
            return convert(mesEquipmentExamineProjectService.selectById(id));
        } catch (Exception e) {
            logger.error("MesEquipmentExamineProject select by id failed.", e);
            throw new RuntimeException("MesEquipmentExamineProject select by id failed, Please order your code.", e);
        }
    }

    @Override
    public MesEquipmentExamineProjectDTO selectObjByQuery(MesEquipmentExamineProjectDTOQuery mesEquipmentExamineProjectDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesEquipmentExamineProjectDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesEquipmentExamineProjectDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(mesEquipmentExamineProjectDTOQuery.getOrderBy())) {
                query.put("orderBy", mesEquipmentExamineProjectDTOQuery.getOrderBy());
            }

            return convert(mesEquipmentExamineProjectService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("MesEquipmentExamineProject select obj by query failed.", e);
            throw new RuntimeException("MesEquipmentExamineProject select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<MesEquipmentExamineProjectDTO> getListForPage(MesEquipmentExamineProjectDTOQuery mesEquipmentExamineProjectDTOQuery) {
        Query query = Query.page(mesEquipmentExamineProjectDTOQuery.getPn(), mesEquipmentExamineProjectDTOQuery.getPageSize());

        if (mesEquipmentExamineProjectDTOQuery.getId() != null && mesEquipmentExamineProjectDTOQuery.getId() > 0) {
            query.put("id", mesEquipmentExamineProjectDTOQuery.getId());
        }

        if (mesEquipmentExamineProjectDTOQuery.getIds() != null && !mesEquipmentExamineProjectDTOQuery.getIds().equals("")) {
            query.put("ids", mesEquipmentExamineProjectDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(mesEquipmentExamineProjectDTOQuery.getLikeName())) {
            query.put("likeName", mesEquipmentExamineProjectDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(mesEquipmentExamineProjectDTOQuery.getShowDate())) {
            query.put("showDate", mesEquipmentExamineProjectDTOQuery.getShowDate());
        }

        if (mesEquipmentExamineProjectDTOQuery.getTableId() != null) {
            query.put("tableId", mesEquipmentExamineProjectDTOQuery.getTableId());
        }

        if (mesEquipmentExamineProjectDTOQuery.getRecordId() != null) {
            query.put("recordId", mesEquipmentExamineProjectDTOQuery.getRecordId());
        }

        if (StringUtils.isNotBlank(mesEquipmentExamineProjectDTOQuery.getStartDate())) {
            query.put("startDate", mesEquipmentExamineProjectDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(mesEquipmentExamineProjectDTOQuery.getEndDate())) {
            query.put("endDate", mesEquipmentExamineProjectDTOQuery.getEndDate());
        }

        if (mesEquipmentExamineProjectDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", mesEquipmentExamineProjectDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(mesEquipmentExamineProjectDTOQuery.getOrderBy())) {
            query.put("orderBy", mesEquipmentExamineProjectDTOQuery.getOrderBy());
        }

        List<MesEquipmentExamineProjectDTO> list = convert(mesEquipmentExamineProjectService.selectByQuery(query));
        Page<MesEquipmentExamineProjectDTO> page = Page.from(mesEquipmentExamineProjectDTOQuery.getPn(), mesEquipmentExamineProjectDTOQuery.getPageSize(), list);
        page.setTotalItems(mesEquipmentExamineProjectService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return mesEquipmentExamineProjectService.deleteById(id);
        } catch (Exception e) {
            logger.error("MesEquipmentExamineProject delete failed.", e);
            throw new RuntimeException("MesEquipmentExamineProject delete failed, Please order your code.", e);
        }
    }
}